#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace zkf
{
	template<class T>
	struct list_node
	{
		T _date;
		list_node<T>* _next;
		list_node<T>* _prev;
		list_node(const	T& s=T())
			:_date(s)
			, _next(nullptr)
			, _prev(nullptr)
		{}
	};
	template<class T,class ref,class ptr>
	struct list_iterator
	{
		typedef list_node<T> node;
		typedef list_iterator<T,ref,ptr> self;
		node* _node;
		list_iterator(node* node)
			:_node(node)
		{}
		ref operator*()
		{
			return _node->_date;
		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self& operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		self& operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		bool operator!=(const self& s)
		{
			return _node!= s._node;
		}
		ptr operator->()
		{
			return &_node->_date;
		}
	};
	template<class T>
	class list
	{
		typedef list_node<T> node;
	public:
		typedef list_iterator<T,T&,T*> iterator;
		typedef list_iterator<T,const T&,const T*> const_iterator;
		void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list()
		{
			empty_init();
		}
		list(const list<T>& s)
		{
			empty_init();
			for (auto& it : s)
			{
				push_back(it);
			}
		}
		void swap(list<T>& s)
		{
			std::swap(_head, s._head);
			std::swap(_size, s._size);
		}
		list<T>& operator=(list<T> s)
		{
			swap(s);
			return *this;
		}
		iterator insert(iterator it, const T& s)
		{
			node* newnode = new node(s);
			node* tail=(it._node)->_prev;
			newnode->_prev = tail;
			newnode->_next = it._node;
			tail->_next = newnode;
			(it._node)->_prev = newnode;
			++_size;
			return newnode;
		}
		iterator erase(iterator it)
		{
			assert(it != end());
			node* prev = (it._node)->_prev;
			node* next = (it._node)->_next;
			prev->_next = next;
			next->_prev = prev;
			delete it._node;
			--_size;
			return next;
		}
		void push_back(const T& s)
		{
			insert(end(), s);
		}
		void push_front(const T& s)
		{
			insert(begin(), s);
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}
		size_t size()const
		{
			return _size;
		}
		bool empty()const
		{
			return _size == 0;
		}
		iterator begin()
		{
			
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin()const
		{

			return _head->_next;
		}
		const_iterator end()const
		{
			return _head;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it=erase(it);
			}
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
	private:
		node* _head; 
		size_t _size;
	};
	template<class container>
	void print_container(container& p)
	{
		for (auto e : p)
		{
			cout << e << " ";
		}
		cout << endl;
	} 
	template<class container>
	void print_container(const container& p)
	{
		for (auto e : p)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}
